<?php
include_once($_SERVER['DOCUMENT_ROOT'] . "/modules/connection.php");

function get_list($prepared) {

    $prepared->execute();
    print("<table class=\"mb_list\">");
    $i = 0;
    while ($result = $prepared->fetch()) {
        ++$i;
        if ($i % CELL_PER_ROW == 1)
            echo "<tr>";

        echo "<td class=\"mb_cell\">";
        echo "<div class=\"mb_data\">";

        echo "<div class=\"tb_img\">";
        echo "<a href = \"show_mobile.php?mbname=" . $result['model'] . "\">";
        echo "<img class=\"mb_img\" src=\"" . $result['picture_path'] . "\" alt=\"MOBILE IMAGE HERE\">";
        echo "<div class=\"mb_name\">" . $result['model'] . "</div>";
        echo "</a>";
        echo "</div>";

        echo "<div class=\"mb_info\">";
        echo "Giá: " . "<b>" . number_format($result['price'], 3, ',', ',') . " VNĐ</b>";
        echo "<br />Số lượng: ";
        if ($result['quantity'] > 0)
            echo "Còn trong kho";
        else
            echo "Hết hàng";
        echo "</div>";

        echo "</div>";
        echo "</td>";

        if ($i % CELL_PER_ROW == 0)
            echo "</tr>";
    }
    print("</table>");
    return $prepared->rowCount();
}

function get_result($query) {
    ?>
    <script type="text/javascript" src="modules/orderlist.js"></script>
    <form class="form_setting" action="#" method="get">
        <fieldset style="padding:10px 10px 10px 10px;">
            <legend>Sắp xếp hiển thị</legend>
            <select id="orderby" style="width: 30%;" name="orderby" onchange="orderlist(this.value,document.getElementById('bywhich').value)">
                <option value="model" <?php if (isset($_REQUEST['orderby']) && $_REQUEST['orderby'] == 'model')
        echo "selected=\"selected\""; ?>>Theo model</option>
                <option value="price" <?php if (isset($_REQUEST['orderby']) && $_REQUEST['orderby'] == 'price')
                        echo "selected=\"selected\""; ?>>Theo giá</option>
            </select>
            <select id="bywhich" style="width: 30%;" name="bywhich" onchange="orderlist(document.getElementById('orderby').value,this.value)">
                <option value="ASC" <?php if (isset($_REQUEST['bywhich']) && $_REQUEST['bywhich'] == 'ASC')
                        echo "selected=\"selected\""; ?>>Thứ tự tăng dần</option>
                <option value="DESC" <?php if (isset($_REQUEST['bywhich']) && $_REQUEST['bywhich'] == 'DESC')
                        echo "selected=\"selected\""; ?>>Thứ tự giảm dần</option>
            </select>
        </fieldset>
    </form>
    <?php
//make query
    if (isset($_GET['orderby']))
        $orderby = $_GET['orderby'];
    else
        $orderby = 'model';
    if (isset($_GET['bywhich']))
        $bywhich = $_GET['bywhich'];
    else
        $bywhich = 'asc';
    $query = $query . " ORDER BY " . $orderby . " " . $bywhich;

//connect
    $conn = connectDB();

//run SQL
    $prepared = $conn->prepare($query);
    $prepared->execute();
    $num = $prepared->rowCount();

    if (isset($_GET['perpage']))
        $limit = $_GET['perpage'];
    else
        $limit = 9;

    if (isset($_GET['page']))
        $page = $_GET['page'];
    else
        $page = 1;
    $offset = $limit * ($page - 1);

    $query.=" limit $limit offset $offset";
    $prepared = $conn->prepare($query);
    get_list($prepared);
    $curnum = $prepared->rowCount();
    $maxpage = (int) (($num - 0.5) / $limit) + 1;
    $from = $offset + 1;
    $to = $offset + $curnum;

//break list into pages
    $url = $_SERVER['QUERY_STRING'];
    if ($url != null) {
        $opos = strripos($url, "orderby");
        $bpos = strripos($url, "bywhich");
        $ppos = strripos($url, "page");
        if ($bpos != false) {
            for ($i = $opos; $i < strlen($url); ++$i) {
                if ($url[$i] == '&')
                    break;
            }
            if ($i == strlen($url) - 1)
                ++$i;
            $url = substr_replace($url, "orderby=" . $orderby, $opos, $i - $opos);
        }
        if ($bpos != false) {
            for ($i = $bpos; $i < strlen($url); ++$i) {
                if ($url[$i] == '&')
                    break;
            }
            if ($i == strlen($url) - 1)
                ++$i;
            $url = substr_replace($url, "bywhich=" . $bywhich, $bpos, $i - $bpos);
        }
        if ($ppos != false) {
            for ($i = $ppos; $i < strlen($url); ++$i) {
                if ($url[$i] == '&')
                    break;
            }
            if ($i == strlen($url))
                $url = substr_replace($url, "", $ppos - 1, $i - $ppos + 2);
            else
                $url = substr_replace($url, "", $ppos, $i - $ppos + 1);
        }
    } else
        $url = "orderby=$orderby&bywhich=$bywhich";
    $url = "?$url";

    echo "<script type=\"text/javascript\" src=\"modules/buttonpic.js\"></script>";
    echo "<div class=\"pagenumber\">";
    echo "<table class=\"pages\"><tr>";
    echo "<td title=\"Kết quả thứ $from-$to / $num\"><div class=\"transbox\" style=\"width:7.5em; text-align:center;\"><img class=\"page_button\" src=\"style/images/pageofpage.png\" alt=\"Trang $page của $maxpage trang\" style=\"width: 7.5em;\"/>Trang $page / $maxpage</div></td>";
    if ($page != 1) {
        echo "<td title=\"Đến trang đầu tiên\"><a href=\"$url&page=1\"><div class=\"transbox\" onmouseover=\"changepic(this)\" onmouseout=\"returnpic(this)\"><img class=\"page_button\" src=\"style/images/first.png\" alt=\"<<\"/></div></a></td>";
        echo "<td title=\"Trở về trang trước\"><a href=\"$url&page=" . ($page - 1) . "\"><div class=\"transbox\" onmouseover=\"changepic(this)\" onmouseout=\"returnpic(this)\"><img class=\"page_button\" src=\"style/images/prev.png\" alt=\"<\"/></div></a></td>";
    }

    if ($page == 1) {
        echo "<td title=\"Đến trang đầu tiên\"><div class=\"transbox\"><img class=\"page_button\" src=\"style/images/first.png\" alt=\"<<\"/></div></td>";
        echo "<td title=\"Trở về trang trước\"><div class=\"transbox\"><img class=\"page_button\" src=\"style/images/prev.png\" alt=\"<\"/></div></td>";
        echo "<td><div class=\"transbox\"><img class=\"page_button\" src=\"style/images/page_button.png\" alt=\"1\"/><b>1</b></div></td>";
        if ($maxpage >= 2)
            echo "<td><a href=\"$url&page=2\"><div class=\"transbox\" onmouseover=\"changepic(this)\" onmouseout=\"returnpic(this)\"><img class=\"page_button\" src=\"style/images/page_button.png\" alt=\"2\"/>2</div></a></td>";
        if ($maxpage >= 3)
            echo "<td><a href=\"$url&page=3\"><div class=\"transbox\" onmouseover=\"changepic(this)\" onmouseout=\"returnpic(this)\"><img class=\"page_button\" src=\"style/images/page_button.png\" alt=\"3\"/>3</div></a></td>";
    } else
    if ($page == $maxpage) {
        if ($maxpage >= 3)
            echo "<td><a href=\"$url&page=" . ($page - 2) . "\"><div class=\"transbox\" onmouseover=\"changepic(this)\" onmouseout=\"returnpic(this)\"><img class=\"page_button\" src=\"style/images/page_button.png\" alt=\"" . ($page - 2) . "\"/>" . ($page - 2) . "</div></a></td>";
        if ($maxpage >= 2)
            echo "<td><a href=\"$url&page=" . ($page - 1) . "\"><div class=\"transbox\" onmouseover=\"changepic(this)\" onmouseout=\"returnpic(this)\"><img class=\"page_button\" src=\"style/images/page_button.png\" alt=\"" . ($page - 1) . "\"/>" . ($page - 1) . "</div></a></td>";
        echo "<td><div class=\"transbox\"><img class=\"page_button\" src=\"style/images/page_button.png\" alt=\"$maxpage\"/><b>$maxpage</b></div></td>";
        echo "<td title=\"Tới trang sau\"><div class=\"transbox\"><img class=\"page_button\" src=\"style/images/next.png\" alt=\">\"/></div></td>";
        echo "<td title=\"Đến trang cuối cùng\"><div class=\"transbox\"><img class=\"page_button\" src=\"style/images/last.png\" alt=\">>\"/></div></td>";
    } else
        for ($i = $page - 1; $i <= $page + 1; ++$i) {
            if ($i == $page)
                echo "<td><div class=\"transbox\"><img class=\"page_button\" src=\"style/images/page_button.png\" alt=\"$i\"/><b>$i</b></div></td>";
            else
                echo "<td><a href=\"$url&page=$i\"><div class=\"transbox\" onmouseover=\"changepic(this)\" onmouseout=\"returnpic(this)\"><img class=\"page_button\" src=\"style/images/page_button.png\" alt=\"$i\"/>$i</div></a></td>";
        }

    if ($page != $maxpage) {
        echo "<td title=\"Tới trang sau\"><a href=\"$url&page=" . ($page + 1) . "\"><div class=\"transbox\" onmouseover=\"changepic(this)\" onmouseout=\"returnpic(this)\"><img class=\"page_button\" src=\"style/images/next.png\" alt=\">\"/></div></a></td>";
        echo "<td title=\"Đến trang cuối cùng\"><a href=\"$url&page=$maxpage\"><div class=\"transbox\" onmouseover=\"changepic(this)\" onmouseout=\"returnpic(this)\"><img class=\"page_button\" src=\"style/images/last.png\" alt=\">>\"/></div></a></td>";
    }

    echo "</tr></table>";
    echo "</div>";
    //close
    closeDB($conn);
    return $num;
}
?>